import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';

import 'dart:typed_data';

void testSocketServer() {
  Isolate.spawn((msg) {
    ServerSocket.bind(InternetAddress.anyIPv4, 9000).then((serverSocket) {
      // HttpServer httpserver = new HttpServer.listenOn(serverSocket);
      // httpserver.forEach((request) {
      //   print('${request.headers}');
      //   request.response.write('hello');
      // });
      serverSocket.listen((Socket socket) {
        print('${socket.port}');
        socket.write('Hello, client.');

        socket.listen((event) {
          print('S-> $event');
        });

        Future.delayed(Duration(seconds: 2), () {
          print('S-> close');
          socket.close();
          socket.destroy();
        });
      });
    });
  }, '');
}

void client() async {
  var socket = await Socket.connect('127.0.0.1', 4041);
  print(socket.runtimeType);
  socket.write('Hello, World,hello dart!');
  print('client send message');
  socket
      .transform(StreamTransformer<Uint8List, String>.fromHandlers(
          handleData: (Uint8List src, Sink sink) {
    sink.add(utf8.decode(src.toList()));
  }, handleDone: (Sink) {
    print("client sink done");
  }))
      .listen((String content) async {
    print('received from server: ${content}');
    socket.destroy();
    print('client already destroy');
    await socket.close();
    print('client already close');
  });
}

void server() async {
  var serverSocket = await ServerSocket.bind('127.0.0.1', 4041);
  print('connected');
  await for (var socket in serverSocket) {
    socket
        .transform(StreamTransformer<Uint8List, String>.fromHandlers(
            handleData: (Uint8List src, Sink dst) {
      dst.add(utf8.decode(src.toList()));
    }, handleDone: (Sink sink) async {
      print('server done');
      await serverSocket.close();
      print('server already close');
    }))
        .listen((String content) {
      print('received from client: ${content}');
      socket.write('success!');
    });
  }
}

void interfaces() async {
  if (!NetworkInterface.listSupported) {
    return;
  }
  print("NetworkInterface.listSupported");

  var list = await NetworkInterface.list();
  list.forEach((v) {
    print('name ${v.name}');
    print('length ${v.addresses.length}');
    v.addresses.forEach((address) {
      print(' address ${address}');
    });
  });
}

void socketClient() async {
  try {
    var socket = await Socket.connect('127.0.0.1', 4041);
    print(socket.runtimeType);
    socket.write('Hello, World,hello dart!');
    print('client send message');
    socket
        .transform(StreamTransformer<Uint8List, String>.fromHandlers(
            handleData: (Uint8List src, Sink sink) {
      sink.add(utf8.decode(src.toList()));
    }, handleDone: (Sink) {
      print("client sink done");
    }))
        .listen((String content) async {
      print('received from server: ${content}');
      socket.destroy();
      print('client already destroy');
      await socket.close();
      print('client already close');
    });
  } catch (e) {
    print(e);
  }
}

Future<void> testClient() async {
  var socket = await Socket.connect('127.0.0.1', 9000);
  socket.listen((Uint8List event) {
    print('-> $event');
  });
  socket.write('Hello, World,hello dart!');
  Future.delayed(Duration(seconds: 11), () {
    print('====');
    socket.write('S-> close');
  });
}

main() {
  // socket();
  // server();
  // client();
  // interfaces();
  testSocketServer();
  testClient();
}
